// pages/product-setting/index.js
import {
  valueType
} from '../../dataSource'

import {
  useDialog
} from '../../miniprogram_npm/miniprogram-vc-components/dialog/useDialog';
import {
  _get,
  update,
  productList,
  productAdd,
  productRemove,
  productUpdate
} from '../../api/setting'

let dialog;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataSource: [],
    valueType: valueType.filter((_, index) => index < 2),
    showSelectData: false,
    zIndex: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchData();
  },
  fetchData() {
    productList({}, res => {
      this.setData({
        dataSource: res
      })
    })
  },
  onNameChange(event) {
    const {
      params,
      value
    } = event.detail;

    const {
      dataSource
    } = this.data;
    dataSource[params[0]].name = value;
    this.setData({
      dataSource
    })
  },

  onAdd() {
    const {
      dataSource
    } = this.data;
    dataSource.push({
      name: '',
      settingsValue: [{
          enabled: false,
          value: ''
        },
        {
          enabled: false,
          value: ''
        },
      ]
    })
    this.setData({
      dataSource
    })
  },
  onSelect(event) {
    const value = event.detail[0],
      {
        dataSource
      } = this.data,
      {
        i,
        index
      } = event.currentTarget.dataset;
    dataSource[index].settingsValue[i].type = value;
    this.setData({
      dataSource
    })
  },
  onCheckChange(event) {
    const value = event.detail,
      {
        dataSource
      } = this.data,
      {
        i,
        index
      } = event.currentTarget.dataset;
    dataSource[index].settingsValue[i].enabled = value;
    this.setData({
      dataSource
    })
  },
  onDelect(event) {
    const dialogInstance = dialog.init({
      title: "",
      content: "确定要移除此配置吗？",
      icon: "icon-warning",
      showCancel: true,
      mask: true
    }, () => {
      const {
        dataSource
      } = this.data, {
        index
      } = event.currentTarget.dataset;
      productRemove(dataSource[index], () => {
        dataSource.splice(index, 1);
        this.setData({
          dataSource
        });
      })
      dialogInstance.close();
    }, () => dialogInstance.close())
    dialogInstance.open();
  },
  onCloseSelectData() {
    this.setData({
      showSelectData: false
    })
  },
  onShowSelectData(event) {
    const instant = this.selectComponent('#select');
    const value = event.currentTarget.dataset.source,
      i = event.currentTarget.dataset.i,
      index = event.currentTarget.dataset.index;

    this.setData({
      showSelectData: true
    })
    instant.setSource(value.name, value['settingsValue'][i]['selectValues'], {
      i,
      index
    });

  },
  onFinish(event) {
    const values = event.detail.data,
      dataSource = this.data.dataSource,
      {
        i,
        index
      } = event.detail.tempData;
    dataSource[index]['settingsValue'][i]['selectValues'] = values;
    this.setData({
      dataSource
    })
    this.onCloseSelectData();

  },
  onSave(event) {
    const item = event.currentTarget.dataset.item;
    item.id ?
      productUpdate(item, () => {
        wx.showToast({
          title: '已保存',
          icon: 'none'
        })
      }) : productAdd(item, () => {
        wx.showToast({
          title: '已添加',
          icon: 'none'
        })
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

    dialog = useDialog('#dialog', this);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})